def restore_ip_addresses(s):
   
    res = []
    path = []
    length = len(s)
    
    if length > 12 or length < 4:
        return res
    
    def backtrack(start, residue):
        # 符合要求：字符已经用完，且为四段
        if start == length and residue == 0:
            res.append('.'.join(path))
            return
        
        for i in range(start, min(start + 3, length)):
            # 剪枝：剩余字符不足以分配给剩余段
            if residue * 3 < length - i:
                continue
                
            # 判断当前段是否是有效的IP段
            if is_ip_segment(s, start, i):
                current_segment = s[start:i+1]
                path.append(current_segment)
                backtrack(i + 1, residue - 1)
                # 回溯
                path.pop()
    
    def is_ip_segment(s, left, right):
        # 如果长度大于1且首位为0，则不合法
        if right - left + 1 > 1 and s[left] == '0':
            return False
            
        # 判断对应数字是否满足范围
        num = 0
        for i in range(left, right + 1):
            num = num * 10 + int(s[i])
        
        return 0 <= num <= 255
    
    backtrack(0, 4)  # 从第0个字符开始，需要4段IP地址
    return res
